package com.fy56.platform.logistics.utils;

import java.text.DecimalFormat;

/**
 * @author li
 * @date 2018-04-20 14:31
 */
public class GoogleMapHelper {

    private static double EARTH_RADIUS = 6378.137;  //地球半径

    //将用角度表示的角转换为近似相等的用弧度表示的角 Math.toRadians
    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * 谷歌地图计算两个坐标点的距离
     * @param lng1  经度1
     * @param lat1  纬度1
     * @param lng2  经度2
     * @param lat2  纬度2
     * @return 距离（千米）
     */
    public static double getDistance(double lng1, double lat1, double lng2, double lat2) {
        double radLat1 = Math.toRadians(lat1);
        double radLat2 = Math.toRadians(lat2);
        double a = radLat1 - radLat2;
        double b = Math.toRadians(lng1) - Math.toRadians(lng2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
                Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
        s = s * EARTH_RADIUS;
        return Double.valueOf(formatDouble(s));
    }

    /**
     * DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers.
     * @param d
     * @return
     */
    public static String formatDouble(double d) {
        DecimalFormat df = new DecimalFormat("#.00");
        return df.format(d);
    }
}
